﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using BestBuy.DAL;
using System.Web.UI.WebControls;

namespace BestBuy.PageUser
{
    public partial class EditProfile : System.Web.UI.Page
    {
        CThongTinCaNhan _tt = new CThongTinCaNhan();
        private bool _flag = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadThongTin();
            }
        }

        protected void LoadThongTin()
        {
            int ss = Convert.ToInt32(Session["IDDN"].ToString());

            ThongTinTaiKhoan tttk = _tt.GetAccountID(ss);

            txtBProduct.Text = tttk.SanPhamMua;
            txtSProduct.Text = tttk.SanPhamBan;
            txtCity.Text = tttk.ThanhPho;
            txtFirst.Text = tttk.HoTen;
            txtPhone.Text = tttk.DienThoai;
            txtMail.Text = tttk.Email;
            txtStreet.Text = tttk.Duong;
            txtProvince.Text = tttk.Tinh;
            txtYahoo.Text = tttk.Yahoo;
            txtSkype.Text = tttk.Skype;
            ddlJob.SelectedValue = tttk.IDCongViec.ToString();
            cbbCountry.SelectedValue = tttk.IDQuocGia.ToString();
            rbtGender.Items.FindByValue(tttk.GioiTinh.Value.ToString().ToLower()).Selected = true;
            if (tttk.LoaiND != null)
            {
                rblType.Items.FindByValue(tttk.LoaiND.Value.ToString().ToLower()).Selected = true;
            }
            if (tttk.LoiMoi != null)
            {
                rblRec.Items.FindByValue(tttk.LoiMoi.Value.ToString().ToLower()).Selected = true;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (_flag)
            {
                ThongTinTaiKhoan tk = new ThongTinTaiKhoan();
                tk.IDTaiKhoan = Convert.ToInt32(Session["IDDN"].ToString());
                tk.Email = txtMail.Text;
                tk.SanPhamMua = txtBProduct.Text;
                tk.SanPhamBan = txtSProduct.Text;
                tk.ThanhPho = txtCity.Text;
                tk.HoTen = txtFirst.Text;
                tk.DienThoai = txtPhone.Text;
                tk.Duong = txtStreet.Text;
                tk.Tinh = txtProvince.Text;
                tk.Yahoo = txtYahoo.Text;
                tk.Skype = txtSkype.Text;
                if (rbtGender.SelectedValue != "-1")
                {
                    tk.GioiTinh = Convert.ToBoolean(rbtGender.SelectedValue);
                }
                bool kq = _tt.UpdateUser(tk);
                if (kq)
                {
                    pnThongBao.Visible = true;
                    pnThongBao.CssClass = "InLeft success message";
                    lbThongBao.Text = "Update successfully.";
                }
                else
                {
                    pnThongBao.Visible = true;
                    pnThongBao.CssClass = "InLeft error message";
                    lbThongBao.Text = "Update failure.";
                }
            }

        }
    }
}